﻿package Interface.ShopClasses
{	
	import caurina.transitions.Tweener;
	import caurina.transitions.Equations;
	
	import flash.display.Sprite;	

	public class ShopContainer extends Sprite
	{
		private const CColumnsPerPage:int = 3;
		private const CRowsPerPage:int = 2;
		private const CItemsPerPage:int = CColumnsPerPage * CRowsPerPage;
		private const CXShift:int = 32;
		private const CYShift:int = 22;
		private const CXPadding:int = 150;
		private const CYPadding:int = 100;
		private const CInitialX:int = 58; 
		private const CPageSize:int = CXShift + CXPadding * CColumnsPerPage;
		private const CTweenTime:int = 1;		
		//variables for items placing
		private var FRow:int;
		private var FColumn:int;
		//variables for scrolling
		private var FCurrentPage:int;
		private var FPagesCount:int;
		private var FItemsCount:int;
		
		public function ShopContainer():void 
		{
			FRow = 0;
			FColumn = 0;
			FPagesCount = 0;
			FCurrentPage = 0;
			FItemsCount = 0;
		}
		
		public function Add(AItem:ShopItem):void
		{
			addChild(AItem);			
			AItem.x = CXShift + CXPadding * FColumn + FPagesCount * CPageSize;
			AItem.y = CYShift + CYPadding * FRow;
			
			FColumn++;
			
			if(FColumn > CColumnsPerPage - 1)
			{
				if(FRow < CRowsPerPage - 1)
				{
					FRow++;					
				}
				else
				{
					FRow = 0;
					FPagesCount++;
				}
				FColumn = 0;
			}
			FItemsCount++;
		}
		
		public function MoveLeft():void
		{
			FCurrentPage--;			
			Tweener.addTween(this, {x:NewX, time:CTweenTime, transition: "easeOutQuint"});
		}
		
		public function MoveRight():void
		{	
			FCurrentPage++;				
			Tweener.addTween(this, {x:NewX, time:CTweenTime, transition: "easeOutQuint"});			
		}
		
		private function get NewX():Number
		{
			return CInitialX - FCurrentPage * CPageSize;
		}
				
		public function get IsFirstPage():Boolean
		{
			return FCurrentPage == 0;
		}
		
		public function get IsLastPage():Boolean
		{
			return FCurrentPage == PagesCount - 1;
		}
		
		public function get PagesCount():int
		{
			return FPagesCount + int(FItemsCount % CItemsPerPage > 0);
		}
		
		public function get Page():int
		{
			return FCurrentPage;
		}
		
		public function set Page(APage:int):void
		{
			FCurrentPage = APage;			
			Tweener.addTween(this, {x:NewX, time:CTweenTime, transition: "easeOutQuint"});
		}
		
		public function Reset():void
		{
			FCurrentPage = 0;			
			x = NewX;
		}
	}	
}
